<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>

    <script type="text/javascript" src="../js/17.0.0.1/react.development.js"></script>
    <script type="text/javascript" src="../js/17.0.0.1/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/17.0.0.1/babel.min.js"></script>
    
    <script type="text/babel">
        class Persion extends React.Component {
            state = {Persion:[
                {id:1,name:"张三",age:18},
                {id:2,name:"里斯",age:32}
            ]}
            render() {
                return (
                    <div>
                        <h2>使用id作为key</h2>
                        <ul>
                            {
                                this.state.Persion.map((persionObj) => {
                                    return <li key={persionObj.id}>{persionObj.name},{persionObj.age} <input type="text" name="" id=""/> </li>
                                })
                            }
                        </ul>
                        <h2>使用index索引作为key引发的问题</h2>
                        <ul>
                            {
                                this.state.Persion.map((persionObj,index) => {
                                    return <li key={index}>{persionObj.name},{persionObj.age} <input type="text" name="" id=""/> </li>
                                })
                            }
                        </ul>
                        <button onClick={this.add}>添加</button>
                    </div>
                )
            }
            add = () => {
                let {Persion} = this.state
                let newPersion = {id:Persion.length+1,name:"王五",age:42}
                this.setState({Persion:[newPersion,...Persion]})
                
            }
        }
        ReactDOM.render(<Persion/>,document.querySelector("#app"))
    </script>
</body>
</html>